package edu.pku.sei.transformation.editor.controller.commands.node;

import org.eclipse.gef.commands.Command;

import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.Transformation;

public class RuleDragDropCheckCommand extends Command {
	private Rule rule;
	private AtomicTransformation transformation;
	public RuleDragDropCheckCommand(AtomicTransformation transformation, Rule rule) {
		this.rule = rule;
		this.transformation = transformation;
	}
	
	@Override
	public boolean canExecute() {
		if (rule == null 
				|| transformation == null 
				|| rule.getOwner() == null 
				|| rule.getOwner() == transformation)
			return false;
		else {
			AtomicTransformation owner = rule.getOwner();
			Transformation t = transformation;
			while(true) {
				if (t.getExtend() == null)
					return false;
				else
					t = t.getExtend().getExtendTransformation();
				
				if (t == null)
					return false;
				else if (t == owner)
					return true;
			}
		}
	}
}
